package com.wqtz.chechai.view.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.QuoteFirstFragmentAdapter;
import com.wqtz.chechai.adapter.QuoteSecondFragmentAdapter;
import com.wqtz.chechai.base.BaseFragment;
import com.wqtz.chechai.bean.ItemType;
import com.wqtz.chechai.bean.Order;
import com.wqtz.chechai.bean.OrderBean;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.ToastUtils;
import com.wqtz.chechai.view.activities.LoanInfoActivity;
import com.yinglan.swiperefresh.PullToRefreshBase;
import com.yinglan.swiperefresh.PullToRefreshSwipeListView;
import com.yinglan.swiperefresh.SwipeMenu;
import com.yinglan.swiperefresh.SwipeMenuCreator;
import com.yinglan.swiperefresh.SwipeMenuItem;
import com.yinglan.swiperefresh.SwipeMenuListView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.type;


/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 13:38
 */

public class QuoteSecondPagerFragment extends BaseFragment {


    /**
     * 需要展示的order数据
     */
    private List<Order> mDatas;
    private PullToRefreshSwipeListView mPullRefreshListView;
    int requsetDataNum = 0;
    private QuoteSecondFragmentAdapter mAdapter;


    @Override
    public BasePresent createPresenter() {
        return null;
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_quote_second;
    }

    @Override
    protected void init(View view) {
        mPullRefreshListView = (PullToRefreshSwipeListView) view.findViewById(R.id.pull_to_refresh_list);
        mDatas = new ArrayList<>();
        mAdapter = new QuoteSecondFragmentAdapter(R.layout.item_quote_second);
        triggerLoadData(16,true);
        mPullRefreshListView.setAdapter(mAdapter);
        mAdapter.setClickListen(new QuoteSecondFragmentAdapter.QuoteItemClickCallBack() {
            @Override
            public void oItemClickItem(int positon) {
                //跳转到放款详情
                toDetailActivity(positon);

            }

            @Override
            public void oItemClickSwipeMenu(int position, int type) {
                switch (type) {
                    case 0:
                        ToastUtils.show(getContext(),"删除",2000);
                        mDatas.remove(position);
                        mAdapter.clearData();
                        mAdapter.addData(mDatas);
                        break;
                    case 1:
                        ToastUtils.show(getContext(),"查看",2000);
                        //跳转到放款详情
                        toDetailActivity(position);
                        break;
                }
            }
        });
        mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<SwipeMenuListView>() {
            //下拉刷新
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(), "下拉刷新", Toast.LENGTH_SHORT).show();
                        //触发加载数据
                        triggerLoadData(10, true);

                    }
                }, 2000);
            }

            //上拉加载
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(), "加载更多", Toast.LENGTH_SHORT).show();
                        //触发加载数据
                        triggerLoadData(10, false);

                    }
                }, 2000);
            }
        });
        mPullRefreshListView.getRefreshableView().setOnMenuItemClickListener(mAdapter);
        //创建侧滑菜单
        mPullRefreshListView.getRefreshableView().setMenuCreator(creator);
    }

    @Override
    protected void loadDatas() {

    }

    private void triggerLoadData(int data, boolean isResfresh) {
        mDatas.clear();
        if (isResfresh) {
            requsetDataNum = 16;
        } else {
            requsetDataNum += data;
        }
        //模拟加载数据
        for (int i = 0; i < requsetDataNum; i++) {
            Order order = new Order();
            order.setLoanStatus(i);
            order.setType(i % 16); //16种类型0-15
            order.setVisible(true);
            order.setLoanName("陈东"+i);
            order.setCarNumber("闵K565656");
            order.setLoanDays("押15天");
            order.setCarInfo("广州本田 雅阁 2.0 EX 2010款");
            order.setLoanInfo("8.6万公里 · 3年10个月 · 一抵");
            order.setLoanTime(i + "分钟前");
            order.setPrice("20.0万");
            mDatas.add(order);
        }
//        loadDatas();
        mAdapter.clearData();
        mAdapter.addData(mDatas);
        mPullRefreshListView.onRefreshComplete();
    }

    private SwipeMenuCreator creator = new SwipeMenuCreator() {


        @Override
        public void create(SwipeMenu menu) {
            // Create different menus depending on the view type
            switch (menu.getViewType()) {
                case 0:
                    createMenu1(menu);
                    break;
                case 1:
                    createMenu2(menu);
                    break;
            }

        }

        /**
         * 创建删除菜单
         * @param menu
         */
        private void createMenu1(SwipeMenu menu) {
            SwipeMenuItem item1 = new SwipeMenuItem(
                    getContext());
            item1.setBackground(R.mipmap.menu_see);
            item1.setWidth(dp2px(90));
            item1.setTitleColor(Color.WHITE);
            item1.setTitleSize(18);
            item1.setTitle(R.string.delete);
            menu.addMenuItem(item1);

        }

        /**
         * 创建查看菜单
         * @param menu
         */
        private void createMenu2(SwipeMenu menu) {
            SwipeMenuItem item1 = new SwipeMenuItem(
                    getContext());

            item1.setWidth(dp2px(90));
            item1.setBackground(R.mipmap.menu_del);
            item1.setTitleColor(Color.WHITE);
            item1.setTitleSize(18);
            item1.setTitle(R.string.look);
            menu.addMenuItem(item1);
        }

        private int dp2px(int dp) {
            return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                    getResources().getDisplayMetrics());
        }
    };


    private void toDetailActivity(int position){
        //跳转到放款详情
        Intent intent = new Intent(getContext(), LoanInfoActivity.class);
        Order order = mDatas.get(position);
        intent.putExtra("type",order.getType());
        startActivity(intent);
    }


}
